package com.huanke.iot.manage.controller.device.wb;

import com.huanke.iot.base.api.ApiResponse;
import com.huanke.iot.base.dto.wb.*;
import com.huanke.iot.base.po.customer.CustomerUserPo;
import com.huanke.iot.base.request.wb.DistributeRequest;
import com.huanke.iot.manage.service.customer.CustomerUserService;
import com.huanke.iot.manage.service.device.wb.MaintenanceService;
import com.huanke.iot.manage.vo.request.BaseRequest;
import com.huanke.iot.manage.vo.request.device.wb.MaintenanceFinishRequest;
import com.huanke.iot.manage.vo.request.device.wb.MaintenanceMaterialRequest;
import com.huanke.iot.manage.vo.request.device.wb.MaintenanceOrderRequest;
import com.huanke.iot.manage.vo.request.device.wb.MaintenanceWbUserRequest;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/wb")
@Slf4j
public class MaintenanceController {

    @Autowired
    private MaintenanceService maintenanceService;

    @Autowired
    private CustomerUserService customerUserService;

    @ApiOperation("查询订单列表")
    @PostMapping(value = "/order/select")
    public ApiResponse<List<MaintenanceOrderVo>> selectList(@RequestBody MaintenanceOrderRequest request) throws Exception {
        CustomerUserPo customerUserPo = customerUserService.selectByManageuserId(request.getManageUserId());
        if(customerUserPo == null || customerUserPo.getId() == null){
            throw new Exception("用户权限错误");
        }
        List<MaintenanceOrderVo> maintenanceOrderVos = maintenanceService.selectList(request, customerUserPo.getCustomerId());
        return new ApiResponse<>(maintenanceOrderVos);
    }

    @ApiOperation("维保人员管理列表")
    @PostMapping(value = "/wbuser/select")
    public ApiResponse<List<MaintenanceManageWbUserVo>> selectWbuserList(@RequestBody MaintenanceWbUserRequest request) throws Exception {
        CustomerUserPo customerUserPo = customerUserService.selectByManageuserId(request.getManageUserId());
        if(customerUserPo == null || customerUserPo.getId() == null){
            throw new Exception("用户权限错误");
        }
        List<MaintenanceManageWbUserVo> maintenanceManageWbUserVos = maintenanceService.selectWbUserList(request, customerUserPo.getCustomerId());
        return new ApiResponse<>(maintenanceManageWbUserVos);
    }

    @ApiOperation("工作中维保人员")
    @PostMapping(value = "/wbuser/working")
    public ApiResponse<List<MaintenanceManageWbUserVo>> selectWorkedWbuserList() throws Exception {
        List<MaintenanceManageWbUserVo> maintenanceManageWbUserVos = maintenanceService.selectWorkedWbUserList();
        return new ApiResponse<>(maintenanceManageWbUserVos);
    }

    @ApiOperation("维保人员添加")
    @PostMapping(value = "/wbuser/add")
    public ApiResponse<Boolean> wbUserAdd(@RequestBody MaintenanceManageWbUserVo request) throws Exception {
        try {
            maintenanceService.addWbUser(request);
            return new ApiResponse<>(true);
        } catch (Exception e) {
            return new ApiResponse<>(500, e.getMessage());
        }
    }

    @ApiOperation("维保人员详情")
    @PostMapping(value = "/wbuserDetail/{wbUserId}")
    public ApiResponse<MaintenanceManageWbUserVo> wbUserAdd(@PathVariable("wbUserId") Integer wbUserId) throws Exception {
        try {
            MaintenanceManageWbUserVo maintenanceManageWbUserVo = maintenanceService.wbuserDetail(wbUserId);
            return new ApiResponse<>(maintenanceManageWbUserVo);
        } catch (Exception e) {
            return new ApiResponse<>(500, e.getMessage());
        }
    }

    @ApiOperation("维保人员修改")
    @PostMapping(value = "/wbuser/update")
    public ApiResponse<Boolean> wbUserUpdate(@RequestBody MaintenanceManageWbUserVo request) throws Exception {
        try {
            if(request.getId() == null){
                throw new Exception("id不能为空");
            }
            maintenanceService.updateWbUser(request);
            return new ApiResponse<>(true);
        } catch (Exception e) {
            return new ApiResponse<>(500, e.getMessage());
        }
    }

    @ApiOperation("维保人员删除")
    @PostMapping(value = "/wbuser/delete")
    public ApiResponse<Boolean> wbUserDelete(@RequestBody BaseRequest<Integer> request) throws Exception {
        maintenanceService.deleteWbUser(request);
        return new ApiResponse<>(true);
    }

    /**
     * 忽略
     * @return
     */
    @ApiOperation("忽略")
    @RequestMapping(value = "/neglect/{orderId}", method = RequestMethod.POST)
    public ApiResponse<Boolean> neglect(@PathVariable Integer orderId) {
        log.info("忽略,orderId->{}", orderId);
        maintenanceService.neglectOrder(orderId);
        return new ApiResponse<>(true);
    }

    /**
     * 管理员派单
     * @return
     */
    @ApiOperation("管理员派单")
    @RequestMapping(value = "/distribute", method = RequestMethod.POST)
    public ApiResponse<Boolean> distribute(@RequestBody DistributeRequest distributeRequest) throws Exception {
        try {
            log.info("管理员派单,orderId->{}", distributeRequest.getOrderId());
            CustomerUserPo customerUserPo = customerUserService.selectByManageuserId(distributeRequest.getManageUserId());
            if(customerUserPo == null || customerUserPo.getId() == null){
                throw new Exception("用户权限错误");
            }
            maintenanceService.distribute(distributeRequest, customerUserPo.getId());
            return new ApiResponse<>(true);
        } catch (Exception e) {
            return new ApiResponse<>(500, e.getMessage());
        }
    }

    /**
     * 查看订单详情
     * @return
     */
    @ApiOperation("查看订单详情")
    @RequestMapping(value = "/orderDetail/{orderId}", method = RequestMethod.GET)
    public ApiResponse<MaintenanceOrderDetailVo> orderDetail(@PathVariable("orderId") Integer orderId) {
        log.info("查看订单详情,orderId->{}", orderId);
        MaintenanceOrderDetailVo maintenanceOrderDetailVo = maintenanceService.orderDetail(orderId);
        return new ApiResponse<>(maintenanceOrderDetailVo);
    }

    /**
     * 指定后台用户为管理员
     * @return
     */
    @ApiOperation("指定后台用户为管理员")
    @RequestMapping(value = "/manageuser/{userId}", method = RequestMethod.POST)
    public ApiResponse<Boolean> orderDetail(@PathVariable("userId") Integer userId, @RequestBody BaseRequest<String> request) throws Exception {
        try {
            log.info("指定后台用户为管理员,userId->{}", userId);
            maintenanceService.manageuser(userId, request);
            return new ApiResponse<>(true);
        } catch (Exception e) {
            return new ApiResponse<>(500, e.getMessage());
        }
    }

    /**
     * 管理员 已完成/已拒绝
     * @return
     */
    @ApiOperation("管理员 已完成/已拒绝")
    @RequestMapping(value = "/finish/{orderId}", method = RequestMethod.POST)
    public ApiResponse<Boolean> finish(@PathVariable("orderId") Integer orderId, @RequestBody MaintenanceFinishRequest request) throws Exception {
        try {
            log.info("管理员 已完成/已拒绝");
            CustomerUserPo customerUserPo = customerUserService.selectByManageuserId(request.getManageUserId());
            if(customerUserPo == null || customerUserPo.getId() == null){
                throw new Exception("用户权限错误");
            }
            maintenanceService.finish(orderId, request.getFinishStatus(), customerUserPo.getId());
            return new ApiResponse<>(true);
        } catch (Exception e) {
            return new ApiResponse<>(500, e.getMessage());
        }
    }

    @ApiOperation("查询订单历史记录列表")
    @PostMapping(value = "/orderHistoryRecord/select")
    public ApiResponse<List<MaintenanceOrderRecordHistoryVo>> selectList(@RequestBody BaseRequest<Integer> request) throws Exception {
        List<MaintenanceOrderRecordHistoryVo> maintenanceOrderRecordHistoryVoList = maintenanceService.selectWbHistoryList(request.getValue());
        return new ApiResponse<>(maintenanceOrderRecordHistoryVoList);
    }


    @ApiOperation("耗材列表")
    @PostMapping(value = "/material/select")
    public ApiResponse<List<MaintenanceMaterialVo>> MaterialList(@RequestBody MaintenanceMaterialRequest request) throws Exception {
        CustomerUserPo customerUserPo = customerUserService.selectByManageuserId(request.getManageUserId());
        if(customerUserPo == null || customerUserPo.getId() == null){
            throw new Exception("用户权限错误");
        }
        List<MaintenanceMaterialVo> maintenanceManageWbUserVos = maintenanceService.selectMaterialList(request, customerUserPo.getCustomerId());
        return new ApiResponse<>(maintenanceManageWbUserVos);
    }
    @ApiOperation("耗材添加")
    @PostMapping(value = "/material/add")
    public ApiResponse<Boolean> materialAdd(@RequestBody MaintenanceMaterialVo request) throws Exception {
        maintenanceService.materialAdd(request);
        return new ApiResponse<>(true);
    }

    @ApiOperation("耗材修改")
    @PostMapping(value = "/material/update")
    public ApiResponse<Boolean> materialUpdate(@RequestBody MaintenanceMaterialVo request) throws Exception {
        maintenanceService.materialUpdate(request);
        return new ApiResponse<>(true);
    }

    @ApiOperation("耗材删除")
    @PostMapping(value = "/material/delete/{materialId}")
    public ApiResponse<Boolean> materialDelete(@PathVariable("materialId") Integer materialId) throws Exception {
        maintenanceService.materialDelete(materialId);
        return new ApiResponse<>(true);
    }

}
